Up vote 1 down vote favorite 1 share g+ share fb share tw.
I have a tab separated file with 16 fields. Can I use awk to conditionally alter one one or fields and print the whole line as the output? E.g.
, depending on value in the first field, I want to say add a specific number to field 4 and so on. But as a output I have to print the whole line. If yes, I would like to know how.
Awk link|improve this question asked Nov 21 '11 at 22:38Sam377312 90% accept rate.
Yes, you can. Do you also need some help with the how? – Rafe Kettler Nov 21 '11 at 22:39 @RafeKettler yes, thank you!
– Sam Nov 21 '11 at 22:41.
Yes, here's an approach: awk '$1 == "ALTER" { $5=$5+5 } 1' With etuardu's sample file, this produces ALTER xx yy zz 15 10 NOALT aa bb cc 20 20 ALTER 11 22 33 35 30 Here's how it works. There are two cases, expressed as awk patterns and actions. In the first, the pattern is a test of the first field to see if it equals "ALTER", invoking the action that modifies the fifth field if the pattern evaluates to true.
The second pattern is that final 1; it's always true, so the implicit print action is performed. One quirk with this solution: the field separators read in the input need not be those in the output. With the simple form above, the field separators from the inputs are taken to be whitespace, while single spaces are written to the output; set FS and OFS to specific values if that isn't what you want.
However, only lines where a field is changed will have the new field separators, so a null op like '$1=$1' will force all records to be rebuilt (this is a convenient way to change field separators, by only using $1=$1).
Nicely done @Michael +1 – Jaypal Singh Nov 22 '11 at 8:22 The 'quirk' is also a problem in the other two answers, so I wouldn't hold it against this one. Great answer (although I would call the 'conditions' 'actions'. ) – schot Nov 22 '11 at 9:00 @schot Yes, that quirk is present in pretty much any solution to this, but I thought it worth mentioning as it is not obvious here.
I'll adjust the text to use the standard awk terminology of patterns and actions; I hope you'll give it another look. – Michael J. Barber Nov 22 '11 at 9:04 @MichaelJ.
Barber You made a great answer even better. – schot Nov 22 '11 at 9:19 @schot Thanks! And thanks for double-checking the changes!
– Michael J. Barber Nov 22 '11 at 9:26.
Yes. This is an example of use (perhaps not the smartest): etuardu@subranu:~$ cat sample. Txt ALTER xx yy zz 10 10 NOALT aa bb cc 20 20 ALTER 11 22 33 30 30 etuardu@subranu:~$ awk '{ if ($1=="ALTER") print $1,$2,$3,$4,$5+1,$6-1; else print $0 }' sample.
Txt ALTER xx yy zz 11 9 NOALT aa bb cc 20 20 ALTER 11 22 33 31 29 Please refer to man awk or some online tutorial (there are plenty) for more informations.
Thanks for the reply. I know this solution. Its just that I don't want to type in print $1,$2....$32 etc. I am looking for a less type intensive solution.
– Sam Nov 22 '11 at 0:20.
How about this (file sample taken from @etuardu)- awk '{if($1=="ALTER") {$5=$5+1;print"\n";for (i=1;i Run a for loop by repeating the loop based on number of fields. Print the fields which would include the updated fields. If the expected field is not found run the else loop.
Sed will remove any blank lines that has been created due to new lines. Sample: jaypal~/Temp$ cat text8 NOALT aa bb cc 20 20 ALTER xx yy zz 10 10 ALTER xx yy zz 10 10 NOALT aa bb cc 20 20 ALTER 11 22 33 30 30 jaypal~/Temp$ awk '{if($1=="ALTER") {$5=$5+1;print"\n";for (i=1;i.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.